package com.carlinfo.shop.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.carlinfo.shop.common.util.ConstatFinalUtil;
import com.carlinfo.shop.common.util.PageInfoUtil;
import com.carlinfo.shop.common.web.controller.BaseController;
import com.carlinfo.shop.users.pojo.AAdmins;
import com.carlinfo.shop.users.service.IUsersService;

/**
 * 后台管理员的Controller
 * 必须登陆以后才能访问/back/
 * @author wangshMac
 */
@Controller
@RequestMapping("/back/users/")
public class UsersBackController extends BaseController
{
	@Resource
	private IUsersService usersService;
	
	/**
	 * 登陆后的首页
	 * 
	 * 想访问此方法,一定得是类上的面requestmapping+方法上面的requestmapping
	 * @return
	 * jsp的路径:jsp路径:前缀+Controller方法的返回值+后缀
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("==main==");
		return "/back/main" ; 
	}
	
	/**
	 * 管理员列表页面
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList()
	{
		ConstatFinalUtil.LOGGER.info("==adminsList==");
		return "/back/adminsList";
	}
	
	/**
	 * 管理员列表页面
	 * 专门用来获取数据,返回值必须是json
	 * @return
	 * 返回值默认是jsp路径的一部分,
	 * 返回值不再是jsp路径的一部分,当成字符串原样返回(ResponseBody)
	 * 
	 * json的格式参照此文件:/easyui/datagrid.json
	 */
	@RequestMapping(value="/adminsListJSON",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String adminsListJSON(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("==adminsListJSON==");
		/* 分页模块 */
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(page,rows);
		
		/* 搜索模块 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		if(keyword == null)
		{
			keyword = ""; 
		}
		keyword = keyword.trim() ; 
		
		if(status == null)
		{
			status = ""; 
		}
		if(st == null)
		{
			st = ""; 
		}
		if(ed == null)
		{
			ed = ""; 
		}
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/* 将字符串变成时间 */
			stDate = this.dateUtil.strToDateTime(st);
			edDate = this.dateUtil.strToDateTime(ed);
		}
		
		/* 搜索+分页+不分页查询多条记录 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		/* 清除map里面的内容 */
		condMap.clear();
		
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		Map<String, Object> resultMap = this.usersService.findCondListService(pageInfoUtil, condMap);
		/* 由于dubbo致命的bug,在消费者的方法参数,如果在生产者(ServiceImpl)发生了修改,消费者(Controller)看不到 
		 * 
		 * PageInfoUtil在ServiceImpl类中设置了总条数,在Controller层看不到
		 * */
		List<AAdmins> adminsList = (List<AAdmins>) resultMap.get("adminsList");
		int totalRecord = Integer.valueOf(resultMap.get("totalRecord") + "");
		pageInfoUtil.setTotalRecord(totalRecord);
		/* 放到四大作用域中 
		 * request.setAttribute();
		 * */
		request.setAttribute("adminsList", adminsList);
		
		/* 拼装easyui需要的数据 */
		JSONObject resultJSON = new JSONObject() ; 
		resultJSON.put("total", pageInfoUtil.getTotalRecord());
		JSONArray adminsArr = new JSONArray() ; 
		
		for (Iterator iterator = adminsList.iterator(); iterator.hasNext();)
		{
			AAdmins admins = (AAdmins) iterator.next();
			/* 如果将admins转换成json */
			JSONObject adminsJSON = admins.toJSON();
			adminsArr.add(adminsJSON);
		}
		
		resultJSON.put("rows", adminsArr);
		return resultJSON.toString();
	}
}
